package com.balabala.gym_system.controller.admin;

import com.balabala.gym_system.pojo.dto.AdvertingBaseDeleteDto;
import com.balabala.gym_system.pojo.dto.AdvertisingDto;
import com.balabala.gym_system.pojo.dto.BaseDeleteDto;
import com.balabala.gym_system.pojo.entity.Advertising;
import com.balabala.gym_system.service.AdvertisingService;
import com.balabala.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/Advertising")
@Api(tags = "广告轮播图管理接口")
public class AdvertisingController {

    @Autowired
    private AdvertisingService advertisingService;

    @GetMapping(value = "/list")
    @ApiOperation(value = "条件查询广告")
    public Result list(AdvertisingDto advertisingDto){
        //查询页面数据
         List<Advertising> advertisingList = advertisingService.findAdByAdvertisingDto(advertisingDto);
         //页面总记录数
         Long count = advertisingService.findAdByAdvertisingDtoCount(advertisingDto);
         return Result.ok(advertisingList,count);
    }

    @GetMapping(value = "/findAll")
    @ApiOperation(value = "查询所有广告")
    public Result findAll(){
        List<Advertising> all = advertisingService.findAll();
        return Result.ok(all,all.stream().count());
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据id查询广告")
    public Result findById(@PathVariable("id") String id){
         Advertising advertising = advertisingService.findById(id);
         return Result.ok(advertising);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "添加广告")
    public Result add(@RequestBody Advertising advertising){
        advertisingService.insertAd(advertising);
        return Result.ok();
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "修改广告")
    public Result update(@RequestBody Advertising advertising){
        advertisingService.updateAd(advertising);
        return Result.ok();
    }

    @GetMapping(value = "/deleteById")
    @ApiOperation(value = "根据id删除广告")
    public Result deleteById(@RequestParam String id){
        advertisingService.deleteById(id);
        return Result.ok();
    }

    @GetMapping(value = "/findByStatus")
    @ApiOperation(value = "根据上线、下线(状态)显示广告")
    public Result deleteByStatus(@RequestParam Integer status){
         List<Advertising> advertisingList = advertisingService.findByStatus(status);
         return Result.ok(advertisingList);
    }

    @PostMapping(value = "/deleteByIds")
    @ApiOperation(value = "根据ids批量删除广告")
    public Result deleteByIds(@RequestBody AdvertingBaseDeleteDto advertingBaseDeleteDto){
        advertisingService.deleteByIds(advertingBaseDeleteDto.getIds());
        return Result.ok();
    }
}
